<?php

/**
 * This is the model class for table "{{product}}".
 *
 * The followings are the available columns in table '{{product}}':
 * @property integer $pro_id
 * @property integer $pro_cid
 * @property integer $pro_uid
 * @property string $pro_name
 * @property integer $pro_pic_id
 * @property string $pro_price
 * @property integer $pro_recommend
 * @property integer $pro_status
 * @property integer $pro_iscomment
 * @property integer $pro_left
 * @property integer $pro_sold
 * @property integer $pro_discount
 * @property integer $pro_created
 * @property integer $pro_update
 * @property string $pro_describe
 */
class Product extends CActiveRecord
{

	//是否允许评论
	const COMMENT_ALLOW = 1;
	const COMMENT_CANCEL = 0;
	
	//是否推荐
	const RECOMMEND_ALLOW = 1;
	const RECOMMEND_CANCEL = 0;
	
	//产品状态
	const STATUS_PUBLISHED = 1;
	const STATUS_LOCK = 0;
	const STATUS_DRAFT = 2;
	const STATUS_TRASH = 3;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return Product2 the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{product}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('pro_cid, pro_name, pro_price, pro_iscomment, pro_status, pro_recommend, pro_left, pro_sold, pro_discount,  pro_describe', 'required'),
			array('pro_cid, pro_uid, pro_pic_id, pro_recommend, pro_left, pro_sold, pro_discount, pro_created, pro_update', 'numerical', 'integerOnly'=>true),
			array('pro_price','numerical'),
			array('pro_name', 'length', 'max'=>200),
//			array('pro_price', 'length', 'max'=>18),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('pro_id, pro_cid, pro_uid, pro_name, pro_pic_id, pro_price, pro_recommend, pro_left, pro_sold, pro_discount, pro_created, pro_update, pro_describe', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'file'	=> array(self::HAS_MANY, 'File', 'file_pid'),
            'folder' => array(self::HAS_ONE,'File', 'file_id'),
			'owner'	=> array(self::BELONGS_TO, 'User', 'pro_uid'),
			'category' => array(self::BELONGS_TO, 'Category', 'pro_cid'),
			'comment' => array(self::HAS_MANY, 'Comment', 'com_proid'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
		
			'pro_id' => Yii::t('product','ID'),
			'pro_cid' => Yii::t('product','Category'),
			'pro_uid' => Yii::t('product','Owner'),
			'pro_name' => Yii::t('product','Product Name'),
			'pro_pic_id' => Yii::t('product','Folder'),
			'pro_price' => Yii::t('product','Price'),
			'pro_recommend' => Yii::t('product','Recommend'),
			'pro_iscomment' => Yii::t('product', 'Comment'),
			'pro_status' => Yii::t('product','Status'),
			'pro_left' => Yii::t('product','Left'),
			'pro_sold' => Yii::t('product','Sold'),
			'pro_discount' => Yii::t('product','Discount'),
			'pro_created'=> Yii::t('product','Create'),
			'pro_update' => Yii::t('product','Update'),
			'pro_describe' => Yii::t('product','Description'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('pro_id',$this->pro_id);
		$criteria->compare('pro_cid',$this->pro_cid);
		$criteria->compare('pro_uid',$this->pro_uid);
		$criteria->compare('pro_name',$this->pro_name,true);
		$criteria->compare('pro_pic_id',$this->pro_pic_id);
		$criteria->compare('pro_price',$this->pro_price,true);
		$criteria->compare('pro_recommend',$this->pro_recommend);
		$criteria->compare('pro_iscomment',$this->pro_iscomment);
		$criteria->compare('pro_status',$this->pro_status);
		$criteria->compare('pro_left',$this->pro_left);
		$criteria->compare('pro_sold',$this->pro_sold);
		$criteria->compare('pro_discount',$this->pro_discount);
		$criteria->compare('pro_created',$this->pro_created);
		$criteria->compare('pro_update',$this->pro_update);
		$criteria->compare('pro_describe',$this->pro_describe,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	public function getRecommendState($state)
	{
		switch ($state)
		{
			case self::RECOMMEND_ALLOW:
				return Yii::t('common','ALLOW');
				break;
			case self::RECOMMEND_CANCEL:
				return Yii::t('common','CANCEL');
				break;
			default:
				return Yii::t('common','ALLOW');
		}
	}
	
	public function generateRecommendStateDropdownList()
	{
		return array(
			self::RECOMMEND_ALLOW => Yii::t('common','ALLOW'),
			self::RECOMMEND_CANCEL => Yii::t('common','CANCEL')
		);
	}
	
	public function getStatusState($state)
	{
		switch ($state)
		{
			case self::STATUS_DRAFT:
				return Yii::t('common','DRAFT');
				break;
			case self::STATUS_LOCK:
				return Yii::t('common','LOCK');
				break;
			case self::STATUS_PUBLISHED:
				return Yii::t('common','PUBLISHED');
				break;
			case self::STATUS_TRASH:
				return Yii::t('common','TRASH');
				break;
			default:
				return Yii::t('common','PUBLISHED');
				break;
		}
	}
	
	public function generateStatusStateDropdownList()
	{
		return array(
			self::STATUS_PUBLISHED => Yii::t('common','PUBLISHED'),			
			self::STATUS_LOCK => Yii::t('common','LOCK'),
			self::STATUS_DRAFT => Yii::t('common','DRAFT'),
			self::STATUS_TRASH => Yii::t('common','TRASH')
		);
	}
	
	public function getCommentState($state)
	{
		switch ($state) {
			case self::COMMENT_ALLOW:
			 	return Yii::t('common','ALLOW');
				break;
			case self::COMMENT_CANCEL:
				return Yii::t('common','CANCEL');
				break;
		}
	}
	
	public function generateCommentStateDropdownList()
	{
		return array(
			self::COMMENT_ALLOW=>self::getCommentState(self::COMMENT_ALLOW),
			self::COMMENT_CANCEL=>self::getCommentState(self::COMMENT_CANCEL)
		);
	}
	
	public function generateProductList()
	{
		$arrlist = array();
		$i = 0;
		$model = self::model()->findAll();
		foreach ($model as $data)
		{
			$arrlist[$data->pro_id] = $data->pro_name;
//			$i++;
		}
		return $arrlist;
	}
	
	public function getDiscountState($discount)
	{
		if($discount > 100)
		{
			return "↑{($discount - 100)}%";
		}
		else if($discount < 100)
		{
			return "↓{(100 - $discount)}%";
		}
		else
		{
			return '-';
		}
	}
	
	
	/**
	 * This is invoked before the record is saved.
	 * @return boolean whether the record should be saved.
	 */
	protected function beforeSave()
	{
		if(parent::beforeSave())
		{
			if($this->isNewRecord)
			{
				$this->pro_created = $this->pro_update=time();
				$this->pro_uid = Yii::app()->user->id;
			}
			else
				$this->pro_update=time();
			return true;
		}
		else
			return false;
	}
}